<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>

<div id="content">
    <?php 
        $arr_id = array();
        $array_urutan = array();
        foreach($result->result() as $row){ 
            $arr_id[] = $row->id_menu;
    ?>
        <div style="vertical-align: middle;-webkit-border-radius: 10px;-moz-border-radius: 10px;border-radius: 10px;background-color:#fff;border:2px solid #000; " >
        <br/>           
        <h1 style="text-align: left; color: #000" >&nbsp;<?php echo $row->menu;?>
            <a class="add" title="Tambah Menu" onclick="tambah_data('<?php echo $row->id_menu;?>', '<?php echo $row->menu;?>');">&nbsp;</a>
            <a class="refresh" title="Expand" id="head_<?php echo $row->id_menu;?>">&nbsp;</a>
        </h1>
        <br/>
        <div style="margin-left: 20px; margin-right: 20px;" class="slide_<?php echo $row->id_menu;?>" name="slide">
            <table class="GridTable">
                <tr class="GridHeader">
                    <td>No.</td>
                    <td colspan="2">Menu</td>
                    <td>Link</td>
                    <td>Action</td>
                </tr>
                <?php 
                    $this_menu = $this->menus->getMenu($row->id_menu);
                    $this_num = $this_menu->num_rows();
                    if($this_num > 0){
                        $no = 1; 
                        $urutan = 0;
                        $first_last = $this->menus->getFirstLast($row->id_menu);
                        foreach($this_menu->result() as $this_row){
                            $urutan++;
                            $array_urutan[$row->id_menu][] = $this_row->id_menu;
                ?>
                    <tr class="GridItem1">
                        <td><?php echo $no++;?></td>
                        <td colspan="2"><?php echo $this_row->menu;?></td>
                        <td><?php echo $this_row->link;?></td>
                        <td>
                            <a class="add_sub" title="Tambah Menu" onclick="tambah_data('<?php echo $this_row->id_menu;?>', '<?php echo $this_row->menu;?>');">&nbsp;</a>
                            <a class="edit" title="Ubah Menu" onclick="ubah_data('<?php echo $row->id_menu;?>', '<?php echo $this_row->id_menu;?>', '<?php echo $this_row->menu;?>');">&nbsp;</a>
                            <?php
                                if ($this_row->is_active) {
                                    echo "<a class=\"setaktif\" title=\"Set Non Aktif\" onclick=\"javascript:setIsActive('" . $this_row->id_menu . "', '0')\">&nbsp;</a>";
                                } else {
                                    echo "<a class=\"setnonaktif\" title=\"Set Aktif\" onclick=\"javascript:setIsActive('" . $this_row->id_menu . "', '1')\">&nbsp;</a>";
                                }
                                
                                if ($this_row->use_db_replika) {
                                    echo "<a class=\"setaktif\" title=\"Unset DB Replika\" onclick=\"javascript:setDBReplika('" . $this_row->id_menu . "', '0')\">&nbsp;</a>";
                                } else {
                                    echo "<a class=\"setnonaktif\" title=\"Set DB Replika\" onclick=\"javascript:setDBReplika('" . $this_row->id_menu . "', '1')\">&nbsp;</a>";
                                }
                                
                                if(($this_row->urutan == $first_last->first || $this_row->urutan != $first_last->last) && $this_num != 1){
                            ?>
                                    <a class="shift_down" title="Geser Menu ke Bawah" 
                                       onclick="shift('<?php echo $this_row->id_menu;?>', <?php echo $urutan;?>, '<?php echo $row->id_menu;?>', 1);">&nbsp;</a>
                            <?php
                                }
                                else
                                    echo "<a class=\"setnonreg\" >&nbsp;</a>";
                                
                                if(($this_row->urutan != $first_last->first || $this_row->urutan == $first_last->last) && $this_num != 1){
                            ?>
                                    <a class="shift_up" title="Geser Menu ke Atas" 
                                       onclick="shift('<?php echo $this_row->id_menu;?>', <?php echo $urutan;?>, '<?php echo $row->id_menu;?>', -1);">&nbsp;</a>
                            <?php    
                                }
                                else
                                    echo "<a class=\"setnonreg\" >&nbsp;</a>";
                            ?>
                        </td>
                    </tr>
                <?php
                            $sub_menu = $this->menus->getMenu($this_row->id_menu);
                            $sub_num = $sub_menu->num_rows();
                            if($sub_num > 0){
                                $sub_urutan = 0;
                                $first_last_sub = $this->menus->getFirstLast($this_row->id_menu);
                                foreach ($sub_menu->result() as $sub_row){
                                    $sub_urutan++;
                                    $array_urutan[$this_row->id_menu][] = $sub_row->id_menu;
                ?>
                                    <tr class="GridItem1">
                                        <td><?php echo $no++;?></td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td><?php echo $sub_row->menu;?></td>
                                        <td><?php echo $sub_row->link;?></td>
                                        <td>
                                            <a class="setnonreg" >&nbsp;</a>
                                            <a class="edit" title="Ubah Menu" onclick="ubah_data('<?php echo $this_row->id_menu;?>', '<?php echo $sub_row->id_menu;?>', '<?php echo $sub_row->menu;?>');">&nbsp;</a>
                                            <?php
                                                if ($sub_row->is_active) {
                                                    echo "<a class=\"setaktif\" title=\"Set Non Aktif\" onclick=\"javascript:setIsActive('" . $sub_row->id_menu . "', '0')\">&nbsp;</a>";
                                                } else {
                                                    echo "<a class=\"setnonaktif\" title=\"Set Aktif\" onclick=\"javascript:setIsActive('" . $sub_row->id_menu . "', '1')\">&nbsp;</a>";
                                                }
                                                
                                                if ($this_row->use_db_replika) {
                                                    echo "<a class=\"setaktif\" title=\"Unset DB Replika\" onclick=\"javascript:setDBReplika('" . $sub_row->id_menu . "', '0')\">&nbsp;</a>";
                                                } else {
                                                    echo "<a class=\"setnonaktif\" title=\"Set DB Replika\" onclick=\"javascript:setDBReplika('" . $sub_row->id_menu . "', '1')\">&nbsp;</a>";
                                                }
                                                
                                                if(($sub_row->urutan == $first_last_sub->first || $sub_row->urutan != $first_last_sub->last) && $sub_num != 1){
                                            ?>
                                                    <a class="shift_down" title="Geser Menu ke Bawah" 
                                                       onclick="shift('<?php echo $sub_row->id_menu;?>', <?php echo $sub_urutan;?>, '<?php echo $this_row->id_menu;?>', 1);">&nbsp;</a>
                                            <?php
                                                }
                                                else
                                                    echo "<a class=\"setnonreg\" >&nbsp;</a>";

                                                if(($sub_row->urutan != $first_last_sub->first || $sub_row->urutan == $first_last_sub->last) && $sub_num != 1){
                                            ?>
                                                    <a class="shift_up" title="Geser Menu ke Atas" 
                                                       onclick="shift('<?php echo $sub_row->id_menu;?>', <?php echo $sub_urutan;?>, '<?php echo $this_row->id_menu;?>', -1);">&nbsp;</a>
                                            <?php    
                                                }
                                                else
                                                    echo "<a class=\"setnonreg\" >&nbsp;</a>";
                                            ?>
                                        </td>
                                    </tr>
                <?php
                                }
                            }
                        }
                    }
                    else {
                ?>
                <tr class="GridItem1">
                    <td colspan="5">Tidak ada data</td>
                </tr>
                <?php } ?>
            </table>
            <br/>
        </div>
        </div>
    <?php } ?>
</div>

<script language="javascript">
    
    var arr_urutan = <?php echo json_encode($array_urutan);?>
    
    jQuery(document).ready(function(){
                
        var obj = <?php echo json_encode($arr_id);?>;
        
        jQuery.each( obj, function( key, value ) {
            
            if(value != '<?php echo $no_expand;?>')
                jQuery(".slide_"+value).hide();
            
            jQuery("#head_"+value).click(
                function(){
                    jQuery(".slide_"+value).slideToggle();
                    jQuery.each( obj, function( key2, value2 ) {
                        if(key != key2)
                            jQuery(".slide_"+value2).hide();
                    });
                }
            );
            jQuery("#head_"+value).mouseover(function() {
                jQuery(".head_"+value).css("background-color", "#D8D8D8");
            });
            jQuery("#head_"+value).mouseout(function() {
                jQuery(".head_"+value).css("background-color", "#fff");
            });
        });

    });
    
    function tambah_data(kode, nama){
        var str;
        try{
            str = arr_urutan[kode].join().replace(/,/g, '%2C');
        }
        catch(ex){
            str = "";
        }
        return GB_showCenter('Tambah Menu untuk '+nama, '<?php echo base_url(); ?>index.php/setting/menus_edit/'+ kode + '/' + str, 500, 1200);
    }
    
    function ubah_data(parent, kode, nama){
        return GB_showCenter('Ubah Menu '+nama, '<?php echo base_url(); ?>index.php/setting/menus_edit/'+ parent + '/' + arr_urutan[parent].join().replace(/,/g, '%2C') + '/'+ kode, 500, 1200);
    }
    
    function setIsActive(kode, flag) {
        if(confirm('Anda yakin akan mengubah status id '+ kode +' ini ?')) {
            location.href = '<?php echo base_url() ?>index.php/setting/menus_setAktif/' + kode + '/'+ flag;
        }
    }
    
    function setDBReplika(kode, flag) {
        if(confirm('Anda yakin akan mengubah status id '+ kode +' ini ?')) {
            location.href = '<?php echo base_url() ?>index.php/setting/menus_setReplika/' + kode + '/'+ flag;
        }
    }
    
    function shift(id, ur, parent, inc){
        location.href = '<?php echo base_url() ?>index.php/setting/menus_shift/' + arr_urutan[parent].join().replace(/,/g, '%2C') + '/'+ id + '_'+ ur + '_' + arr_urutan[parent][ur + inc - 1] + '_' + (ur + inc) + '_' + parent;
    }
    
</script>
